﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Forums
{
    public partial class ReportPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fldReturnUrl.Value = Request.UrlReferrer.ToString();
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //TODO Fix alert
            if (txtReportComments.Text.Length != 0)
            {
                lblReportValidator.Visible = false;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CentForumsDB"].ConnectionString);
                cmd.CommandText = string.Format("INSERT INTO dbo.report(report_msg, user_accnt, guideline_id, topic_id, date_reported) VALUES ('{0}',{1},{2},{3}, getdate())", txtReportComments.Text, Session["user"], cboGuidelineBroken.SelectedValue, Request.QueryString["topicId"]);
                try
                {
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    txtReportComments.Text = string.Empty;
                    //Add some javascript to show a confirmation alert and bring the user back to the page they reported from.
					Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", string.Format("alert('Your report has been sent!'); window.location='{0}';", fldReturnUrl.Value), true);
                    //If we don't want to show the alert, comment out the line above and uncomment the line below.
					//Response.Redirect(fldReturnUrl.Value);
                    
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
            else
            {
                lblReportValidator.Visible = true;
            }

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            lblReportValidator.Visible = false;
            txtReportComments.Text = string.Empty;
        }


    }
}
